<?php
App::uses('AppController', 'Controller');
/**
 * Carts Controller
 *
 * @property Cart $Cart
 */
class CartsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Cart->recursive = 0;
		$this->set('carts', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Cart->exists($id)) {
			throw new NotFoundException(__('Invalid cart'));
		}
		$options = array('conditions' => array('Cart.' . $this->Cart->primaryKey => $id));
		$this->set('cart', $this->Cart->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
          //  $customerid = $this->Session->read('Auth.User.id');
         //   $this->request->data['Order']['customer_id'];
          //  debug($this->request->data);
			$this->Cart->create();
			if ($this->Cart->save($this->request->data)) {
         //       debug($this->request->data);
				$this->Session->setFlash(__('The cart has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cart could not be saved. Please, try again.'));
			}
		}
		$customers = $this->Cart->Customer->find('list');
		$items = $this->Cart->Item->find('list');
		$this->set(compact('customers', 'items'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Cart->exists($id)) {
			throw new NotFoundException(__('Invalid cart'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Cart->save($this->request->data)) {
				$this->Session->setFlash(__('The cart has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cart could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Cart.' . $this->Cart->primaryKey => $id));
			$this->request->data = $this->Cart->find('first', $options);
		}
		$customers = $this->Cart->Customer->find('list');
		$items = $this->Cart->Item->find('list');
		$this->set(compact('customers', 'items'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Cart->id = $id;
		if (!$this->Cart->exists()) {
			throw new NotFoundException(__('Invalid cart'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Cart->delete()) {
			$this->Session->setFlash(__('Cart deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Cart was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

    public function addItemToCart($item_id){
        //Is the customer login?
        if ($this->userlogged()) {
            //Yes, add item to DBcart
            //Find users Cart Id
            $cart = $this->Cart->find('first', array('conditions' => array('customer_id' => $this->getLoggedUserId())));
            //pr($this->getLoggedUserId()); die();
            $this->loadModel('Item');
            $item = $this->Item->find('first', array('conditions' => array('Item.id' => $item_id)));

            //Add Items to CartsItem with this car  t id.
            //create data array
            $cartItem = array("cart_id" => $cart['Cart']['id'], "item_id" => $item_id, "price" => $item['Item']['original_price']);
            $this->loadModel('CartsItem');
            $this->CartsItem->create();
            if($this->CartsItem->save($cartItem)) {
                echo('OK'); exit();
            } else {
                echo("FAILED"); exit();
            }
        }
            //No, does the session cart already exist
            $sessionUserId = $this->Session->check('Auth.User.id');
            if ($this->Session->check('$sessionUserId') != true){
                //No, create session cart with item details
                $sessionCart = $this->Cart->create();
            }else{
            //Yes, array push item to session cart
            $sessionCart = array("Item_id" => $item_id,"price" => $item['Item']['original_price']);
            }

    }
}
